<?php
/**
 * Get videos of ranking (top 20)
 * Send an Object in JSON with:
 * - lastupdate: (string) last update in format d/m/Y h:i
 * - videos: (array) Array of Mikubook_Video
 *
 * If an error occurend, send an object with:
 * - error_type: (string) type of error, mainly 'connection'
 * - error: (string) the error
 */
require_once("../include/setup.php");
require_once(CONTROLLER_PATH.'mikubook.controller.php');

Mikubook_Video_Controller::$LOGIN = $_REQUEST['login'];
Mikubook_Video_Controller::$PASSWORD = $_REQUEST['password'];

$c = new Mikubook_Video_Controller();
$videos = $c->getVideos(array('ranktype' => $_GET['type'], 'cached' => true));
foreach ($videos as $v) {
	$v->linkEmbed = $v->getLinkEmbed(array('autoplay' => 1, 'showinfo' => 0));
}
$c->close();

$o = array(
	'lastupdate' => date('d/m/Y H:i', $c->getLastUpdateVideosRank($_GET['type'])),
	'videos' => $videos
);

if (!$c->connected) {
	$o = array(
		'error_type' => 'connection',
		'error' => $c->error_connection
	);
}

echo json_encode($o);
?>